package ui.graphic;

import Modelos.ModeloTabuleiro;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import static java.awt.Component.BOTTOM_ALIGNMENT;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;
import javax.swing.filechooser.FileFilter;
import static ui.graphic.Frame.isInteger;

/**
 *
 * @author gustavodinis
 */
public final class InitMenu extends JPanel implements Observer {

    ModeloTabuleiro mT;
    Image logo;
    URL logoPath;
    JButton botoes[], nPlayers, adicionar, ia;
    JLabel nIALabel;
    final JFileChooser fc;
    BoxLayout layout;
    SpringLayout layout2;
    int nMaxPlayers = 5, nPtemp, nIA, nMinPlayers = 2;
    ArrayList<JTextField> pNames;

    public InitMenu(ModeloTabuleiro mT) {
        repaint();
        this.mT = mT;
        nPtemp = 0;
        nIA = 0;
        pNames = new ArrayList();
        layout = new BoxLayout(this, BoxLayout.Y_AXIS);
        setLayout(layout);
        add(Box.createVerticalGlue());
        logoPath = getClass().getResource("resources/images/splash.jpg");
        try {
            logo = ImageIO.read(logoPath);
        } catch (IOException ex) {
            System.out.println("imagem erro");
        }

        botoes = new JButton[3];
        botoes[0] = new JButton("Novo Jogo");
//        botoes[1] = new JButton("Novo Jogo (Humano vs Computador)");
//        botoes[2] = new JButton("Novo Jogo (Computador vs Computador)");
        botoes[1] = new JButton("Carregar Jogo");
        botoes[2] = new JButton("Sair");

        for (int i = 0; i < 3; i++) {
//            Dimension x = new Dimension(200, 20);
            add(botoes[i]);
            botoes[i].setAlignmentX(Component.RIGHT_ALIGNMENT);
            botoes[i].setFont(new Font("MontereyMediumFLF", Font.PLAIN, 15));
        }
        //String xpto= getClass().getResource("SaveGames").getPath();
        fc = new JFileChooser(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath() + "SaveGames/");
        motionList();
        setVisible(true);
        mT.addObserver(this);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(logo, 0, 0, 675, 443, this);
        g2d.setFont(new Font("MontereyMediumFLF", Font.PLAIN, 15));
    }

    @Override
    public void update(Observable o, Object arg) {
        repaint();
    }

    public void namePlayers() {
        if (nIA + nPtemp < nMaxPlayers) {
            JLabel label1 = new JLabel("Nome: ");
            label1.setOpaque(true);
            label1.setBackground(Color.gray);
            pNames.add(new JTextField());

            label1.setFont(new Font("MontereyMediumFLF", Font.PLAIN, 15));
            add(label1);
            add(pNames.get(pNames.size() - 1));
            revalidate();
            repaint();
            nPtemp++;
        }
    }

    public void motionList() {

        final MouseAdapter loadGame, pXia, addP, addIA, exit, gravarNamePlayer;

        gravarNamePlayer = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (nPtemp + nIA >= nMinPlayers) {
                    for (int i = 0; i < pNames.size(); i++) {
                        if (pNames.get(i).getText().isEmpty()) {
                            for (; i > 0; i--) {
                                pNames.get(i).setText(null);
                            }
                            return;
                        }
                    }
                    mT.start(nIA + nPtemp, pNames);
                }
            }
        };
        addP = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (adicionar == null) {
                    adicionar = new JButton("Começar");
                    //adicionar.setAlignmentX(BOTTOM_ALIGNMENT);
                    add(adicionar);

                }
                adicionar.addMouseListener(gravarNamePlayer);
                namePlayers();
            }
        };
        addIA = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (nIA + nPtemp < nMaxPlayers) {
                    if (adicionar == null) {
                        adicionar = new JButton("Começar");
                        //adicionar.setAlignmentX(BOTTOM_ALIGNMENT);
                        add(adicionar);
                    }
                    nIA++;
                    nIALabel.setText("Numero de IA's: " + nIA);
                    adicionar.addMouseListener(gravarNamePlayer);
                    revalidate();
                    repaint();
                }
            }
        };

        pXia = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                removeAll();
                nPlayers = new JButton("Adicionar Player");
                add(nPlayers);
                nPlayers.addMouseListener(addP);
                ia = new JButton("Adicionar IA");
                add(ia);
                nIALabel = new JLabel("Numero de IA's: " + nIA);
                nIALabel.setOpaque(true);
                nIALabel.setBackground(Color.gray);
                add(nIALabel);
                ia.addMouseListener(addIA);
                revalidate();
                repaint();
            }
        };
        botoes[0].addMouseListener(pXia);

        loadGame = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                readFile();
            }
        };
        botoes[1].addMouseListener(loadGame);

        exit = new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                System.exit(0);
            }
        };
        botoes[2].addMouseListener(exit);

    }
    
   

    public void readFile() {
        int returnVal = fc.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            if (file.getName().toLowerCase().endsWith("save")) {
                System.out.println("Carregado!");
                JOptionPane.showMessageDialog(getParent(), "Ficheiro Carregado!", "Sucesso!", 0);
            } else {
                JOptionPane.showMessageDialog(getParent(), "Ficheiro Não Pode Ser Carregado!\nApenas .save", "Erro", JOptionPane.ERROR_MESSAGE);
            }
            //This is where a real application would open the file.

        } else {
        }
    }

}
